package com.example.medical_delivery_system.api.admin;

import com.example.medical_delivery_system.api.admin.param.BatchIdParam;
import com.example.medical_delivery_system.api.admin.param.ProductAddParam;
import com.example.medical_delivery_system.api.admin.param.ProductEditParam;
import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.config.annotation.TokenToAdminUser;
import com.example.medical_delivery_system.entity.AdminUserToken;
import com.example.medical_delivery_system.entity.Product;
import com.example.medical_delivery_system.service.AdminProductService;
import com.example.medical_delivery_system.util.BeanUtil;
import com.example.medical_delivery_system.util.PageQueryUtil;
import com.example.medical_delivery_system.util.Result;
import com.example.medical_delivery_system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Param：20201003013
 * Author：李镇荣
 * Data：2022/12/18
 */
@RestController
@Api(value = "v1", tags = "8-2.后台管理系统商品模块接口")
@RequestMapping("/manage-api/v1")
public class AdminProductAPI {


    private static final Logger logger = LoggerFactory.getLogger(AdminProductAPI.class);

    @Resource
    AdminProductService productService;

    /**
     * 列表
     */
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    @ApiOperation(value = "商品列表", notes = "商品列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return ResultGenerator.genFailResult("分页参数异常！");
        }
        Map params = new HashMap(4);
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(productService.getProductPage(pageUtil));
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/products", method = RequestMethod.POST)
    @ApiOperation(value = "新增商品", notes = "新增商品")
    public Result save(@RequestBody @Valid ProductAddParam productAddParam, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        Product product = new Product();
        BeanUtil.copyProperties(productAddParam, product);
        String result = productService.saveProduct(product);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/products", method = RequestMethod.PUT)
    @ApiOperation(value = "修改商品信息", notes = "修改商品信息")
    public Result update(@RequestBody ProductEditParam productEditParam, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        Product product = new Product();
        BeanUtil.copyProperties(productEditParam, product);
        String result = productService.updateProduct(product);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/products/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取单条商品信息", notes = "根据id查询")
    public Result info(@PathVariable("id") Long id, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        Product product = productService.getProductById(id);
        if (product == null) {
            return ResultGenerator.genFailResult(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return ResultGenerator.genSuccessResult(product);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/products", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除商品信息", notes = "批量删除商品信息")
    public Result delete(@RequestBody BatchIdParam batchIdParam, @TokenToAdminUser AdminUserToken adminUser) {
        logger.info("adminUser:{}", adminUser.toString());
        if (batchIdParam == null || batchIdParam.getIds().length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (productService.deleteBatch(batchIdParam.getIds())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }
}
